package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.pojo.User;
import com.woniuxy.pojo.dto.AgeDistributionDTO;
import com.woniuxy.pojo.dto.GenderStatisticsDTO;
import com.woniuxy.pojo.dto.RegionUserCountDTO;
import com.woniuxy.service.UserService;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userCount")
public class UserCountController {
    @Autowired
    UserService userService;
    @GetMapping("/statistics")
    public ResponseEntity<Map<String, Object>> getUserStatistics() {
        long memberCount = userService.count(new QueryWrapper<User>().ne("user_level", "普通"));
        long nonMemberCount = userService.count(new QueryWrapper<User>().eq("user_level", "普通"));

        Map<String, Object> data = new HashMap<>();
        data.put("memberCount", memberCount);
        data.put("nonMemberCount", nonMemberCount);
        data.put("total", memberCount + nonMemberCount);

        return ResponseEntity.ok(data);
    }

    @GetMapping("/regionStatistics")
    public List<RegionUserCountDTO> getRegionStatistics() {
        return userService.getUserCountByRegion();
    }

    @GetMapping("/genderStatistics")
    public GenderStatisticsDTO genderStatistics() {
        return userService.getGenderStatistics();
    }

    @GetMapping("/ageDistribution")
    public ResponseEntity<List<AgeDistributionDTO>> getAgeDistribution() {
        List<AgeDistributionDTO> result = userService.getAgeDistributionWithPercentage();
        return ResponseEntity.ok(result);
    }
}
